package boofbridgeimaj.image.util;

import boofcv.struct.image.ImageFloat32;
import boofcv.struct.image.MultiSpectral;
import org.openimaj.image.FImage;
import org.openimaj.image.MBFImage;

/**
 * Class for converting or wrapping BoofCV data types into OpenIMAJ data types.
 *
 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
 */
public class ImajToBoof {

	/**
	 * Converts a {@link MBFImage} from OpenIMAJ into {@link MultiSpectral} in BoofCV.
	 *
	 * @param input MBFImage which is to be converted.
	 * @param output (Optional) Storage for MultiSpectral.  If null a new instance will be created.
	 * @return A copy of the {@link MBFImage} using {@link #convert(FImage, ImageFloat32)} on each band
	 */
	public static MultiSpectral<ImageFloat32> convert( MBFImage input , MultiSpectral<ImageFloat32> output )
	{
		int numBands = input.numBands();
		if( output == null ) {
			output = new MultiSpectral<ImageFloat32>(ImageFloat32.class, input.getWidth(), input.getHeight(), numBands);
		}

		for( int i = 0; i < numBands; i++ )
			convert(input.getBand(i),output.getBand(i));

		return output;
	}

	/**
	 * Converts a {@link FImage} from OpenIMAJ into {@link ImageFloat32} in BoofCV.
	 *
	 * @param input FImage which is to be converted.
	 * @param output (Optional) Storage for ImageFloat32.  If null a new instance will be created.
	 * @return A copy of the {@link FImage#pixels} flattened into the {@link ImageFloat32#data} array using {@link System#arraycopy(Object, int, Object, int, int)}
	 */
	public static ImageFloat32 convert( FImage input , ImageFloat32 output ) {
		if( output == null ) {
			output = new ImageFloat32(input.width, input.height);
		}

		int outIndex = 0;
		for (int y = 0; y < input.height; y++)
		{
			System.arraycopy(input.pixels[y], 0, output.data, outIndex, input.width);
			outIndex+=input.width;
		}

		return output;
	}
}
